package com.haohan.cloud.scm.api.common.req.pay;

import com.haohan.cloud.framework.utils.JacksonUtils;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.*;

/**
 * Created by zgw on 2018/1/25.
 */
@Data
public class OrderPayParamsExt implements Serializable {

    private String uid;

    private String userName;

    private String telephone;

    private String address;

    private String remark;

    private String attr;

  //商品类型 JisuAppConstant.GoodsType
    private String goodsType;

    public OrderPayParamsExt() {
    }

    public Map<String, String> fromAttr() {

        if (StringUtils.isNotEmpty(this.attr)) {
            Map<String, String> params = JacksonUtils.readMapValue(this.attr, String.class);
            return params;
        }
        return new HashMap<>();

    }



    public static Map<String, Object> toMap(JSONObject jsonobj) throws JSONException {
        Map<String, Object> map = new HashMap<String, Object>();
        Iterator<String> keys = jsonobj.keys();
        while (keys.hasNext()) {
            String key = keys.next();
            Object value = jsonobj.get(key);
            if (value instanceof JSONArray) {
                value = toList((JSONArray) value);
            } else if (value instanceof JSONObject) {
                value = toMap((JSONObject) value);
            }
            map.put(key, value);
        }
        return map;
    }

    public static List<Object> toList(JSONArray array) throws JSONException {
        List<Object> list = new ArrayList<Object>();
        for (int i = 0; i < array.length(); i++) {
            Object value = array.get(i);
            if (value instanceof JSONArray) {
                value = toList((JSONArray) value);
            } else if (value instanceof JSONObject) {
                value = toMap((JSONObject) value);
            }
            list.add(value);
        }
        return list;
    }
}
